OS_LOWER := $(shell uname -s | tr '[:upper:]' '[:lower:]')

ifeq ($(OS_LOWER),linux)
	PRE := x86_64-linux-gnu-
else
	PRE := x86_64-elf-
endif

LDFLAGS :=  -m elf_i386 -T link.ld -g
NASMFLAGS := -f elf32 -g -F dwarf -I src/include

qemu: disk.img
	qemu-system-i386 -m 128M -drive file=$<,index=0,media=disk,format=raw

debug: disk.img
	qemu-system-i386 -m 128M -s -S -drive file=$<,index=0,media=disk,format=raw

disk.img: build/os.elf
	${PRE}objdump -x -d -S -M intel build/os.elf > build/os_dis.txt 
	${PRE}readelf -a  build/os.elf > build/os_elf.txt 
	$(PRE)objcopy -O binary build/os.elf build/os.bin
	dd if=/dev/zero of=$@ bs=1M count=16
	dd if=build/os.bin of=$@ conv=notrunc

build/os.elf: build/boot.o build/setup.o
	$(PRE)ld $(LDFLAGS) -o $@ $^

build/boot.o: src/boot/boot.asm
	mkdir -p build
	nasm $(NASMFLAGS) -o $@ $<

build/setup.o: src/boot/setup.asm
	mkdir -p build
	nasm $(NASMFLAGS) -o $@ $<

.PHONY: clean
clean:
	rm -rf build
	rm -rf disk.img
